const path = require('path');
const VueLoaderPlugin = require('vue-loader/lib/plugin');
const { DefinePlugin } = require('webpack');
const fs = require('fs');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const {getConfig} = require(path.resolve(__dirname, 'util/configUtil.js'));

// Read the project information from package.json
const packageJson = JSON.parse(fs.readFileSync(path.resolve('./package.json'), 'utf8'));
const projectName = `"${packageJson.name}"`
const projectVersion = `"${packageJson.version}"`

module.exports = {
    entry: path.resolve(__dirname, 'entry/index.js'),
    resolve: {
        alias: {
            '@': path.resolve('.'),
            '@dist': path.resolve('./dist'),
            '@src': path.resolve('./src'),
            '@@': path.resolve('./src'),
            '@@main': path.resolve('./src/main'),
            '@@serve': path.resolve('./src/serve'),
            '@@test': path.resolve('./src/test'),
        },
        extensions: ['.js', '.vue'],
    },
    devServer:  {
        host: '0.0.0.0',
        port: getConfig("devServer.port"),
        open: getConfig("devServer.open"),
        hot: true,
    },
    module: {
        rules: [
            {
                test: /\.js$/,
                exclude: /(node_modules|bower_components)/,
                use: {
                    loader: 'babel-loader',
                    options: {
                        presets: ['@babel/preset-env'],
                        plugins: ['@babel/transform-runtime'],
                    },
                },
            },
            {
                test: /\.vue$/,
                loader: 'vue-loader',
            },
            {
                test: /\.css$/,
                use: [
                    'style-loader',
                    'css-loader',
                ],
            },
        ],
    },
    mode: "development",
    plugins: [
        new VueLoaderPlugin(),
        new DefinePlugin({
            'project.name': projectName,
            'project.version': projectVersion,
        }),
        new HtmlWebpackPlugin({
            template: path.resolve(__dirname, 'entry/index.html'),
            title: 'Your App Title',
        }),
    ],
};
